<?php

namespace App\Admin\Controllers;

use App\Admin\Actions\FindLineUser;
use App\Admin\Actions\RechargeMoney;
use App\Admin\Actions\RechargeRepeatMoney;
use App\Admin\Actions\SetUserArea;
use App\Admin\Actions\ShareUser;
use App\Admin\Actions\StatusUser;
use App\Admin\Actions\UpdateIdentity;
use App\Admin\Actions\UpdateLeader;
use App\Admin\Actions\UpdatePid;
use App\Admin\Actions\UpdateRealStore;
use App\Admin\Extension\Expand\UsersChild;
use App\Admin\Repositories\TongUser;
use App\Models\TongUserAreaLog;
use Dcat\Admin\Admin;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Widgets\Alert;
use SuperEggs\DcatDistpicker\DcatDistpickerHelper;

class TongUserController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new TongUser(), function (Grid $grid) {
           // $grid->header(Alert::make('1.用户身份为公司或合伙人的要及时设置他们的区域地址，以免用户下单造成区域奖励没有的问题！<br/> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(1.)399首单 分公司10元 合伙人5元 ，399复购单 分公司5元 合伙人2元<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(2.)3980首单 分公司100元 合伙人50元', '注意：'));
            $grid->column('id','会员编号')->sortable();
            $grid->model()->orderBy('id', 'desc');
            $grid->column('nickname');
            $grid->column('mobile');
            $grid->column('money','钱包')->display(function($value){
                return $value.'元';
            });
            $grid->column('repeat_money','提货券')->display(function($value){
                return $value.'元';
            });
            $grid->column('share_code');
            $grid->column('p_id')->display(function($value){
                if($value==0){
                    return '顶级用户';
                }else{
                    $res = \App\Models\TongUser::query()->where('id',$value)->first();
                    return $res['nickname'];
                }

            });
            $grid->column('p_id','上级ID')->display(function($value){
                return $value;
            });
//            $grid->column('child_id', '我得邀请')
//                ->display('查看所有下级')
//                ->expand(UsersChild::make());
            $grid->column('identity_id','用户身份')->view('admin.users.fields.identity_id');
            $grid->column('child_month_order','本月业绩');
            $grid->column('is_leader','领导人')->display(function($value){
                if($value==0){
                    return '<span class="badge bg-danger">非领导</span>';
                }else{
                    return '<span class="badge bg-success">领导人</span>';
                }
            });
            $grid->column('is_real_store','后台指定店铺')->display(function($value){
                if($value==-1){
                    return '<span class="badge bg-danger">未指定</span>';
                }else{
                    return '<span class="badge bg-success">已指定</span>';
                }
            });

            $grid->column('province_id','区域设置')->display(function($value) use($grid){
                $area_res = TongUserAreaLog::query()->where('u_id',$this->id)->get()->toArray();

                if(empty($area_res)){
                    return '<span class="badge bg-danger">未设置</span>';
                }else{
                    $str = '<span class="badge bg-success">已设置</span>';
                    foreach ($area_res as $key=>$val){
                        $str.='<div>'.DcatDistpickerHelper::getAreaName($val['province_id']).'-'.DcatDistpickerHelper::getAreaName($val['city_id']);
                        if(!empty($val['area_id'])){
                            $str.='-'.DcatDistpickerHelper::getAreaName($val['area_id']);
                        }
                    }
                    return $str.'</div>';
                    //return '<span class="badge bg-success">已设置</span><div>'.DcatDistpickerHelper::getAreaName($this->province_id).'-'.DcatDistpickerHelper::getAreaName($this->city_id).'-'.DcatDistpickerHelper::getAreaName($this->area_id).'</div>';
                }
            });

            $grid->column('created_at');
            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->panel();
                $filter->equal('id','会员编号')->width(3);
                $filter->like('p_id', '上级编号')->width(3);
                $filter->like('nickname', '昵称')->width(3);
                $filter->like('mobile', '手机号')->width(3);
                $filter->equal('identity_id','身份')->select([0=>'普通用户',1 => '会员', 2 => '创客', 3 => '瞳小店',4=>'健康馆',5=>'区县代',6=>'市代',7=>'合伙人'])->width(3);
            });

            //禁用掉了批量删除
            $grid->tools(function ($tools) {
                $tools->batch(function ($batch) {
                    $batch->disableDelete();
                });

            });
            //$grid->tools('钱包充值');

            // 禁用添加按钮
            $grid->disableCreateButton();
            $grid->actions(function (Grid\Displayers\Actions $actions) {
                $actions->disableDelete();
                $actions->disableEdit();
                $actions->disableQuickEdit();
                $actions->disableView();
                if(!Admin::user()->isRole('经理') && !Admin::user()->isRole('财务') && !Admin::user()->isRole('发货员')){
                    $actions->append(RechargeMoney::make());
                    $actions->append(RechargeRepeatMoney::make());
                }


                $actions->append(UpdateRealStore::make());
                $actions->append(UpdateIdentity::make());
                $actions->append(UpdatePid::make());
                $actions->append(StatusUser::make());
                $actions->append(UpdateLeader::make());
                $actions->append(FindLineUser::make());
                $actions->append(ShareUser::make());
                if($this->identity_id==5 || $this->identity_id==6){
                    $actions->append(SetUserArea::make());
                }
            });
            //去除掉操作栏
            //$grid->showActions(false);

        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new TongUser(), function (Show $show) {
            $show->field('id');
            $show->field('head_img');
            $show->field('nickname');
            $show->field('mobile');
            $show->field('share_code');
            $show->field('p_id');
            $show->field('child_id');
            $show->field('identity_id');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new TongUser(), function (Form $form) {
            $form->display('id');
            $form->text('head_img');
            $form->text('nickname');
            $form->text('mobile');
            $form->text('share_code');
            $form->text('p_id');
            $form->text('child_id');
            $form->text('identity_id');

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
